package org.csu.jpetstore.bo;

import lombok.Data;
import org.csu.jpetstore.entity.Account;
import org.csu.jpetstore.entity.Signon;
import org.csu.jpetstore.vo.AccountVO;

/**
 * 包含的 entity : Account Signon
 *
 * @author huangzhangyan
 */
@Data
public class AccountBO {
    private String username;
    private String password;
    private String email;
    private String firstname;
    private String lastname;
    private String province;
    private String city;
    private String county;
    private String detailAddress;
    private String phone;
    private String status;

    public AccountBO() {

    }

    public static AccountBO toAccountBO(Account account, Signon signon) {
        //不匹配判断
        if (account == null || signon == null) {
            if (!(account == null && signon == null)) {
                //可能原因 数据库数据有问题,不完整
                throw new RuntimeException();
            }
            return null;
        }
        if (!account.getUserid().equals(signon.getUsername())) {
            //可能原因 查询结果有问题
            throw new RuntimeException();
        }
        AccountBO accountBO = new AccountBO();
        accountBO.setUsername(account.getUserid());
        accountBO.setPassword(signon.getPassword());
        accountBO.setEmail(account.getEmail());
        accountBO.setFirstname(account.getFirstname());
        accountBO.setLastname(account.getLastname());
        accountBO.setProvince(account.getProvince());
        accountBO.setCity(account.getCity());
        accountBO.setCounty(account.getCounty());
        accountBO.setDetailAddress(account.getDetailAddress());
        accountBO.setPhone(account.getPhone());
        accountBO.setStatus(account.getStatus());
        return accountBO;
    }

    public static AccountVO toAccountVO(AccountBO accountBO) {
        if (accountBO == null) {
            return null;
        }
        AccountVO accountVO = new AccountVO();
        accountVO.setUsername(accountBO.getUsername());
        accountVO.setEmail(accountBO.getEmail());
        accountVO.setFirstname(accountBO.getFirstname());
        accountVO.setLastname(accountBO.getLastname());
        accountVO.setProvince(accountBO.getProvince());
        accountVO.setCity(accountBO.getCity());
        accountVO.setCounty(accountBO.getCounty());
        accountVO.setDetailAddress(accountBO.getDetailAddress());
        accountVO.setPhone(accountBO.getPhone());
        // AccountVO 没有 status
        return accountVO;
    }

    public static Account toAccountEntity(AccountBO accountBO) {
        if (accountBO == null) {
            return null;
        }
        Account accountEntity = new Account();
        accountEntity.setUserid(accountBO.getUsername());
        accountEntity.setEmail(accountBO.getEmail());
        accountEntity.setFirstname(accountBO.getFirstname());
        accountEntity.setLastname(accountBO.getLastname());
        accountEntity.setProvince(accountBO.getProvince());
        accountEntity.setCity(accountBO.getCity());
        accountEntity.setCounty(accountBO.getCounty());
        accountEntity.setDetailAddress(accountBO.getDetailAddress());
        accountEntity.setPhone(accountBO.getPhone());
        accountEntity.setStatus(accountBO.getStatus());
        return accountEntity;
    }

    public static Signon toSignonEntity(AccountBO accountBO) {
        Signon signonEntity = new Signon();
        signonEntity.setUsername(accountBO.getUsername());
        signonEntity.setPassword(accountBO.getPassword());
        return signonEntity;
    }
}
